

package com.hd.assistant.web.home.module.screen.json;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.requestcontext.buffered.BufferedRequestContext;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.hd.assistant.biz.excel.ExportExcel;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.dal.dataobject.ordermining.Reports;
import com.hd.assistant.web.common.base.ordermining.BaseTaobaoOrderModule;
/**
 *
 * @author Hangding
 */
public class ExportDailyReportExcel extends BaseTaobaoOrderModule{
    private final static Logger log = LoggerFactory.getLogger(ExportDailyReportExcel.class);
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private BufferedRequestContext buffered;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpSession session;
    
    
   
    public void execute(
    		@Param("type") Integer type,
    		@Param("dateFrom") String dateFrom,
    		@Param("dateTo") String dateTo,
    		Context context, Navigator nav) {
    	 
    	
    	buffered.setBuffering(false);
    	 
		 
    	try{
    		User u = checkPermision(User.Type_TAOBAO_ORDER, session, context, nav);
    		if(u == null)
    			return;
    	
    		if(type != null ){
    			if(dateFrom == null){
    				long l = System.currentTimeMillis() - 30*24*3600*1000l;
    				dateFrom = new SimpleDateFormat("yyyyMMdd").format(new Date(l));
    			}
    			List<Reports> result = this.orderMiningManager.getReportsByUserId(u.getUserId(), type, dateFrom, dateTo);
    			if(result != null)
    			{
    				String fileName = URLEncoder.encode("每日报告下载_"+u.getUserNick(), "UTF-8"); 
    				response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
    		   		 response.setContentType("application/msexcel;charset=utf-8");

    				//response.reset();
    				ExportExcel<Reports> excel = new ExportExcel<Reports>();
    				String[] fields = new String[]{"date","tradesNum","buyerNum","itemNums","totalSales","totalPostFee","totalCreditCost","totalTmallFee","totalSales"};
    				String[] names = new String[]{"日期","成交订单数","成交买家数","成交宝贝数","总销售额（不含邮）","总邮费","信用卡支出","商城佣金","总利润"};
    				excel.exportExcel("日成交报表", fields, names, result, response.getOutputStream(), "yyyyMMdd");
    				//generateOutputExcel(response.getOutputStream(),result);
    				
    				
    			}
    		}
    		
    	}catch(Exception e){
    		log.error("error Itemcosts ",e);
    	}
	    	
    }

	
}
